package com.woniuxy.mq01.configuration;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author author
 * @Create 2023-09-28 11:32
 */
@Configuration
public class RabbitConfiguration {

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    //声明队列
    @Bean
    public Queue queuehellworld(){
        return new Queue("queuehelloworld");
    }
    @Bean
    public Queue queueemail(){
        return new Queue("queueemail");
    }
    @Bean
    public Queue queuesms(){
        return new Queue("queuesms");
    }

    //定义交换机,发布订阅模式使用fanout类型交换机
    @Bean
    public FanoutExchange exchangefanout(){
        return new FanoutExchange("exchangefanout");
    }

    //bytpye    byName
    //将队列绑定到交换机
    @Bean
    public Binding bindingqueueemailtoexchangefanout(Queue queueemail, FanoutExchange exchangefanout){
//        return BindingBuilder.bind(queueemail()).to(exchangefanout());
        return BindingBuilder.bind(queueemail).to(exchangefanout);
    }
    @Bean
    public Binding bindingqueuesmstoexchangefanout(Queue queuesms, FanoutExchange exchangefanout){
        return BindingBuilder.bind(queuesms).to(exchangefanout);
    }

    //路由模式,使用direct类型交换机
    @Bean
    public DirectExchange exchangedirect(){
        return new DirectExchange("exchangedirect");
    }

    @Bean
    public Binding bindingqueueemailtoexchangedirect(Queue queueemail, DirectExchange exchangedirect){
        return BindingBuilder.bind(queueemail).to(exchangedirect).with("email");
    }

    @Bean
    public Binding bindingqueuesmstoexchangedirect(Queue queuesms, DirectExchange exchangedirect){
        return BindingBuilder.bind(queuesms).to(exchangedirect).with("sms");
    }


    //topisc模式使用topic类型的交换机
    @Bean
    public TopicExchange exchangetopic(){
        return new TopicExchange("exchangetopic");
    }

    @Bean
    public Binding bindingqueueemailtoexchangetopic(Queue queueemail, TopicExchange exchangetopic){
        return BindingBuilder.bind(queueemail).to(exchangetopic).with("#.email.#");
    }

    @Bean
    public Binding bindingqueuesmstoexchangetopic(Queue queuesms, TopicExchange exchangetopic){
        return BindingBuilder.bind(queuesms).to(exchangetopic).with("#.sms.#");
    }
}
